
package cn.wolfcode;

/**
 * @author : 刘昊淇
 * @date : 2025/8/13 20:54
 */
public class QuickSort {
    void test(){
        System.out.println("test");
    }
    public static void main(String[] args) {
        int[] arr = new int[]{2,4,5,7,6,1,3};
        quicksort(arr,0,arr.length - 1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
    public static void quicksort(int[] arr,int low,int high){
        if (low < high){
            int pivotIndex = partition(arr,low,high);
            quicksort(arr,low,pivotIndex - 1);
            quicksort(arr,pivotIndex + 1,high);
        }
    }
    private static int partition(int[] arr, int low, int high) {
        int left = low + 1;
        int right = high;
        int pivot = arr[low];
        while (left <= right){
            while (left <= right && arr[left] <= pivot){
                left ++;
            }
            while (left <= right && arr[right] > pivot){
                right --;
            }
            if (left < right){
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
            }
        }
        int temp = arr[low];
        arr[low] = arr[right];
        arr[right] = temp;
        return right;
    }
}